from django.contrib.auth.models import User
from django.db import models

# Create your models here.
CATEGORY = (
    ('food', '⻝品'),
    ('mobile', '⼿机'),
    ('electronics', '电器'),
    ('book', '⽣活⽤品'),
    ('edutainment', '教育娱乐'),
    ('social_services', '社会服务'),
)


class Product(models.Model):
    name = models.CharField(verbose_name='商品', max_length=100, null=True)  # 长按ctrl时CharField会发蓝点进去看原码
    category = models.CharField(verbose_name='商品分类', max_length=50, choices=CATEGORY, null=True)
    quantity = models.PositiveIntegerField('数量', null=True)  # 正整数，verble内容可以省略，
    created = models.DateTimeField(auto_now_add=True)  # Datetime创建日期到时分秒，True是创建的第一时期

    class Meta:  # meta嵌套表
        verbose_name = '商品'  # 汉化表的本身
        verbose_name_plural = verbose_name
        ordering = ['-created']  # 排序

    def __str__(self):  # 魔法方法，调用类中类时返回这个名字的汉语；-代表权限，两个几乎就自己用，-代表权限高；self将类中所有的公共区域传输进self中
        return self.name  # 返回商品名


class Order(models.Model):  # 订单表是从上面的字段中取回的；两张表是关联的，删除一个数据两张表中的数据一起消失
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='产品名称', null=True)  # Foreginkey是一对多
    staff = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='客户', null=True)
    order_quantity = models.PositiveIntegerField(verbose_name='订单数量', null=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:  # 嵌套类，嵌套类必有缩进
        verbose_name = '订单'
        verbose_name_plural = verbose_name
        ordering = ['-created']     # 排序， ordering = ['-created','staff'] 就按两个排序


    def __str__(self):
        return f'{self.staff}订{self.product}-{self.order_quantity}个'       # 拼接变量和
        # return f'{self.staff}员工定了{self.product}商品{self.order_quantity}个'      # 拼接变量和常量的拼接方式
